<?php

/**
 * View class
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 * @author Kofel <kofels@gmail.com>
 * @copyright 2008 (C) by Kofel
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
 * @todo
 */
 
class viewLayer
{
	private $OPT;
	private $Output;
	
	public function load( $n = false )
	{
		require_once 'lib/opt/opt.class.php';
		
		$this->OPT = new optClass;
		$this->OPT->root = 'view/';
		$this->OPT->compile = 'view/temp/';

		$this->OPT->httpHeaders(OPT_HTML); 
		$this->OPT->registerPhpFunction('date', 'date');
		$this->OPT->registerPhpFunction('time', 'time');
		$this->OPT->registerPhpFunction('uc_words', 'capitalize'); 
		$this->OPT->registerPhpFunction('Layer', 'Kernel::layer'); 
        $this->OPT->registerPhpFunction('_', 'Kernel::layer->get');

	}
	
	public function render( $string )
	{
		$this->OPT->root = 'view/' . Kernel::layer('config')->get('cms_template') . '/';
		if( file_exists( $this->OPT->root . 'echo.html' ) )
		{
			$this->OPT->assign('_echoString_', $string);
			$this->Output .= $this->OPT->fetch('echo.html');
		}
		else
		{
			throw new KernelException('Failed to load <strong>echo.html</strong>, check it exist.');
		}
	}
	
	public function sub( $name )
	{
		$this->OPT->root = 'view/' . Kernel::layer('config')->get('cms_template') . '/';
		$this->Output .= $this->OPT->fetch( $name . '.html' );
	}

	public function set( $name, $value )
	{
		$this->OPT->assign( $name, $value );
		return true;
	}
	
	public function setTemplate( $name )
	{
		$this->OPT->root = 'view/' . $name . '/';
	}
	
	public function unload( $name = 'index' )
	{
		if( $name == 'index' )
		{
			$this->OPT->root = 'view/' . Kernel::layer('config')->get('cms_template') . '/';
		}
		
		$this->OPT->assign( '_output_', $this->Output );
		$this->OPT->parse( $name . '.html' );
	}
}
?>